/*
 * Copyright (c) Cliff Rowley, James Deakin
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of the <ORGANIZATION> nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import acid.util.Proxy;

/**
 * Simple binding class.
 * 
 * Object.watch() is a semi powerful feature of Flash (made much more powerful
 * in Flash 9) that allows a callback to be set when a property of an object
 * changes.  The main limitation is that only one callback may be set (since
 * unwatch() will remove all callbacks for a specified property).  This class
 * attemts to bridge that gap and provide a convenient method of binding the
 * properties of one class to the properties of another.  This will form the
 * basis of the data binding functionality in the user interface.
 * 
 * @author crowley <cliffrowley@gmail.com> */
class acid.util.Binding
{
	
	private var __sObj:Object;
	private var __sProp:String;
	private var __dObj:Object;
	private var __dProp:String;
	
	private function Binding(sObj:Object, sProp:String, dObj:Object, dProp:String)
	{
		__sObj = sObj;
		__sProp = sProp;
		__dObj = dObj;
		__dProp = dProp;
	}
	
	// --------------------------------------------------------------------
	
	private static var __bindings:Array;
	
	/**
	 * Creates a Binding between two properties of two objects.
	 * @param sObj The source object.
	 * @param sProp The name of the source property.
	 * @param dObj The destination object.
	 * @param dProp The name of the destination property.
	 */
	public static function bind(sObj:Object, sProp:String, dObj:Object, dProp:String):Void
	{
		var found:Boolean = false;
		for (var i:Number = 0; i < __bindings.length; i++) {
			if (__bindings[i].source == sObj && __bindings[i].property == sProp) {
				__bindings[i].listeners.push({destination:dObj, property:dProp});
				found = true;
				break;
			}
		}
		
		if (found == false) {
			var binding:Object = {source:sObj, property:sProp, listeners:[]};
			binding.listeners.push({destination:dObj, property:dProp});
			__bindings.push(binding);
			
			var handler:Object = new Object();
			handler.onChange = function(prop:String, oldValue:Object, newValue:Object):Void {
				sObj[sProp] = newValue;
			};
			sObj.watch(sProp, Proxy.create(handler, handler.onChange));
		}
	}
	
	public static function unbind(sObj:Object, sProp:String, dObj:Object, dProp:String):Void
	{
		for (var i:Number = 0; i < __bindings.length; i++) {
			if (__bindings[i].source == sObj && __bindings[i].property == sProp) {
				for (var j:Number = 0; j < __bindings[i].listeners.length; j++) {
					var listener = __bindings[i].listeners[j];
					if (listener.destination == dObj && listener.property == dProp) {
						__bindings[i].listeners.splice(j, 1);
						if (__bindings[i].listeners.length == 0) {
							sObj.unwatch(sProp);
						}
						break;
					}
				}
				break;
			}
		}
	}
	
}
